package com.adgox.tiantianbiting.redpacket.index;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.base.BasicFragment;
import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.bean.WelfareItemBean;
import com.adgox.tiantianbiting.mine.login.LoginActivity;
import com.adgox.tiantianbiting.mine.tingCoinRecord.TingCoinRecordActivity;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.redpacket.rank_income.RankingIncomeActivity;
import com.adgox.tiantianbiting.redpacket.rank_listen.RankingListenActivity;
import com.adgox.tiantianbiting.redpacket.share.share_for_free.ShareForFreeActivity;
import com.adgox.tiantianbiting.redpacket.sign.SignActivity;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.WebviewActivity;
import com.bumptech.glide.Glide;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class WelfareFragment extends BasicFragment {


    //    @BindView(R.id.iv_rank_listen)
//    ImageView ivRankListen;
//    @BindView(R.id.iv_rank_income)
//    ImageView ivRankIncome;
    Unbinder unbinder;
    Unbinder unbinder1;
    @BindView(R.id.grid_view)
    GridView gridView;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    private List<WelfareItemBean> datas;

    public static WelfareFragment newInstance() {
        return new WelfareFragment();
    }


    @Override
    public int getLayoutId(Bundle var1) {
        return R.layout.fragment_welfare;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    private void init() {
        if (SessionManager.getDefault().isLogin()) {
            UserInfo user = SessionManager.getDefault().getUser();
            tvTips.setText("您还有" + user.getCommission() + "元现金没有提现");
        }
        OkGo.<BaseResponseBean<List<WelfareItemBean>>>get(ApiConstant.BASE_URL + ApiConstant.GET_WELFARE_ITEM)
                .params("platform", 2)
                .execute(new JsonCallback<BaseResponseBean<List<WelfareItemBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<List<WelfareItemBean>>> response) {
                        if ("0".equals(response.body().getCode()) && response.body().getData() != null) {
                            setGridViewData(response.body().getData());
                        }
                    }
                });
    }

    private void setGridViewData(final List<WelfareItemBean> data) {
        datas = data;
        String[] from = {"iv"};
        int[] to = {R.id.iv};
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();

            map.put("iv", datas.get(i).getImageUrl());
            maps.add(map);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(getContext(), maps, R.layout.item_grid_view_welfare, from, to);
        simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Object data, String textRepresentation) {
                if (view instanceof ImageView) {
                    ImageView iv = (ImageView) view;
                    Glide.with(getContext()).load(data.toString()).into(iv);
                    return true;
                } else {
                    return false;
                }
            }
        });
        gridView.setAdapter(simpleAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!SessionManager.getDefault().isLogin()) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                if (data.get(position).getType() == 1) {
                    if ("无h5地址".equals(data.get(position).getRedirectUri())) {
                        return;
                    }
                    Intent intent = new Intent(getActivity(), WebviewActivity.class);
                    intent.putExtra("title",data.get(position).getDescribe());
                    intent.putExtra("url", data.get(position).getRedirectUri());
                    startActivity(intent);
                } else if (data.get(position).getType() == 0) {

                    switch (data.get(position).getRedirectUri()) {
                        case "收听排行榜":
                            getContext().startActivity(new Intent(getActivity(), RankingListenActivity.class));
                            break;
                        case "收入排行榜":
                            getContext().startActivity(new Intent(getActivity(), RankingIncomeActivity.class));
                            break;
                        case "分享免费听":
                            getContext().startActivity(new Intent(getActivity(), ShareForFreeActivity.class));
                            break;
                        case "签到红包":
                            getContext().startActivity(new Intent(getActivity(), SignActivity.class));
                            break;

                    }
                }
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder1 = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick(R.id.iv_check)
    public void onCheckClicked() {
        if (SessionManager.getDefault().isLogin()){
            startActivity(new Intent(getActivity(), TingCoinRecordActivity.class));
        } else {
            startActivity(new Intent(getActivity(),LoginActivity.class));
        }
    }

//    @OnClick(R.id.iv_rank_listen)
//    public void onListenClicked() {
//        startActivity(new Intent(getActivity(), RankingListenActivity.class));
//    }
//
//    @OnClick(R.id.iv_rank_income)
//    public void onIncomeClicked() {
//        startActivity(new Intent(getActivity(), RankingIncomeActivity.class));
//    }
//
//    @OnClick(R.id.iv_sign)
//    public void onSignClicked() {
//        startActivity(new Intent(getActivity(), SignActivity.class));
//    }
//
//    @OnClick(R.id.tv_share)
//    public void onShareClicked() {
//        startActivity(new Intent(getActivity(), ShareForFreeActivity.class));
//    }
}
